package com.itasura.multidatasource.web;

import com.itasura.multidatasource.entity.User;
import com.itasura.multidatasource.mapper.data1.User1Mapper;
import com.itasura.multidatasource.mapper.data2.User2Mapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private User1Mapper user1Mapper;

    @Autowired
    private User2Mapper user2Mapper;

    /**
     * curl -X GET http://127.0.0.1:8080/getUsers
     */
    @RequestMapping("/getUsers")
    public List<User> getUsers() {
        log.info("data1 获取用户列表");
        return user1Mapper.getAll();
    }


    /**
     * curl -X GET http://127.0.0.1:8080/getUser/3
     */
    @RequestMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Long id) {
        log.info("data2 根据id查询用户");
        return user2Mapper.getByPrimaryId(id);
    }

    /**
     * curl -X GET http://127.0.0.1:8080/add?name=zhangsan&sex=1
     * curl -X GET http://127.0.0.1:8080/add?name=lisi&sex=1
     */

    @RequestMapping("/add")
    public void save(User user) {
        log.info("data2 添加用户");
        user2Mapper.insert(user);
    }

    /**
     * curl -X GET http://127.0.0.1:8080/update?id=1&name=sansan
     */
    @RequestMapping(value = "update")
    public void update(User user) {
        log.info("data2 修改用户");
        user2Mapper.update(user);
    }

    /**
     * curl -X GET http://127.0.0.1:8080/delete/1
     */
    @RequestMapping(value = "/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        log.info("data1 删除用户");
        user1Mapper.delete(id);
    }


}